<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>礼包列表</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
	<link rel="stylesheet" href="//at.alicdn.com/t/font_tnyc012u2rlwstt9.css" media="all" />
</head>
<body class="childrenBody">
	<blockquote class="layui-elem-quote news_search">
		<form class="layui-form">

			<div class="layui-inline" id="dynamicShow">
				<label class="layui-form-label">门店</label>
				<div class="layui-input-block">
					<select name="merchantId" id="merchantId">
						<option value="">--请选择门店--</option>
					</select>
				</div>
			</div>
        <div class="layui-inline">
            <label class="layui-form-label">保单号</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="code" placeholder="请输入保单号">
            </div>
        </div>
        
         <div class="layui-inline">
            <label class="layui-form-label">手机号码</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="phone" placeholder="请输入手机号码">
            </div>
        </div>
        
        <!--  <div class="layui-inline">
            <label class="layui-form-label">车牌号</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="categoryName" placeholder="请输入服务类别名称">
            </div>
        </div> -->
        <div class="layui-inline">
            <a class="layui-btn search_btn" lay-submit="" lay-filter="queryPackage">查询</a>
        </div>
       <!--  <div class="layui-inline">
           <a class="layui-btn layui-btn-normal categorysAdd_btn">添加服务类别</a>
        </div> -->
    </form>
		
	</blockquote>
	<div class="layui-form news_list">
	  	<table class="layui-table" id="packList" lay-filter="packs"></table>
	</div>
	<div id="page"></div>
	<script type="text/javascript" src="js/jquery-1.11.2.min.js"></script>
	<script type="text/javascript" src="plugins/layui/layui.all.js"></script>
	<script type="text/javascript" src="js/user/newsList.js"></script>
	
		    <script type="text/html" id="barDemo">
		  <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="iconfont icon-edit"></i>编辑</a>
	    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon"></i>删除</a>
	    
	  </script>
	<script type="text/javascript">
	//重新渲染表单
	function renderForm(){
	  layui.use('form', function(){
	   var form = layui.form;//高版本建议把括号去掉，有的低版本，需要加()
	   form.render();
	  });
	 }
	
	layui.use(['table','form'], function(){
	    var table = layui.table;
	    var form = layui.form;//高版本建议把括号去掉，有的低版本，需要加()
	    //方法级渲染
        $.ajax({
            type : "GET",
            url: '/merchants/merchants.json',
            dataType:'json',
            async:false,
            success: function(data){
                var list=data.data;
                var html='';
                for(var i=0;i<list.length;i++){
                    html += "<option value='"+list[i].id+"'>"+list[i].name+"</option>"
                }
                $("#merchantId").append(html);
                form.render();
            }
        });

        var isAdminstr=localStorage.getItem("isAdmin");
        var isAdmin=isAdminstr=='true'?true:false;
        if(!isAdmin){
            $("#dynamicShow").hide();
        }
	    var tableIns=table.render({
	        elem: '#packList'
	        ,url: '/giftPackages'
	        ,cols: [[
	            {checkbox: true, fixed: true}
//	            ,{field:'id', title: 'ID', width:'10%',  fixed: true}
                ,{field:'belongMerchantName', title: isAdmin?'所属门店':'', width:isAdmin?'10%':'0%',minWidth:'0%',type:isAdmin?'':'space',style:isAdmin?'':'display:none'}
	            ,{field:'code', title: '礼包号',  width:'20%',}
	            ,{field:'phone', title: '手机号码',  width:'10%'}
	            ,{field:'policyCode', title: '保单号',  width:'10%'}
	            ,{field:'status', title: '状态',  width:'10%',
	        	   templet:function(d){
	            		if(d.status==0){
	            			return "<font color='red'>未发送 </font>";
	            		}else{
	            			return "<font color='green'>已发送 </font>";
	            		}
	            	}
	           	}
	            ,{field:'define1', title: '操作',
	            	 templet:function(d){
	            		if(d.status==0){
	            			return "<button type='button' class='layui-btn layui-btn-sm toNsDetail' data-id='"+d.id+"' data-mid='"+d.belongMerchantId+"'>发礼包 </button>";
	            		}else{
	            			return "<button type='button' class='layui-btn layui-btn-sm toDetail' data-id='"+d.id+"' data-mid='"+d.belongMerchantId+"'>查看礼包 </button>";
	            		}
	            	} 
	            
	            }

	        ]]
	        ,id: 'testReload'
	        ,page: true
//	        ,height: 600
	    });
	    
	    
	    $(document).on('click','.toDetail',function(){
	    	var id= $(this).attr('data-id');
            var mid= $(this).attr('data-mid');
	    	toDetail(id,mid);
	     }); 
	    
	    $(document).on('click','.toNsDetail',function(){
	    	var id= $(this).attr('data-id');
	    	var mid= $(this).attr('data-mid');
	    	toNsDetail(id,mid);
	     }); 
	    
	    form.on("submit(queryPackage)", function (data) {
	        var policyCode = data.field.code;
	        var phone = data.field.phone;
            var merchantId = data.field.merchantId;
	        //表格重新加载
	        tableIns.reload({
	            where:{
	            	code:policyCode,
	            	phone:phone,
                    merchantId:merchantId
	            }
	        });

	        return false;
	    });
	    
	    table.on('tool(categorys)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
	    	  var data = obj.data; //获得当前行数据
	    	  var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
	    	  var tr = obj.tr; //获得当前行 tr 的DOM对象
	    	 
	    	  if(layEvent === 'detail'){ //查看
	    	    //do somehing
	    	  } else if(layEvent === 'del'){ //删除
	    	    layer.confirm('真的删除该类别吗', function(index){
	    	      obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
	    	      layer.close(index);
	    	      //向服务端发送删除指令
	    	      var ids=obj.data.id;
	    	      $.ajax({  
	    	            type: "POST",  
	    	            url:"/categorys/delete",  
	    	            data:{id:ids},// 序列化表单值  
	    	            success: function(data1) {  
	    	            	if(data1=='success'){
	    	            		top.layer.close(index);
	    	          			top.layer.msg("删除成功");
	    	           		//	layer.closeAll("iframe");
	    	          	 		//刷新父页面
	    	          	 	//	parent.location.reload();
	    	            	}
	    	            
	    	            } ,
	    	 		
	    		 		 error: function(request) {  
	    		             alert("Connection error");  
	    		         } 
	    	        }); 
	    	    });
	    	  } else if(layEvent === 'edit'){ //编辑
	    	    //do something
	    	     var ids=obj.data.id;
	    	     editCategory(ids);
	    	    
	    	 /*    //同步更新缓存对应的值
	    	    obj.update({
	    	      username: '123'
	    	      ,title: 'xxx'
	    	    }); */
	    	  }
	    	});
	    
	    
	    function editCategory(id){
	        var index = layui.layer.open({
	            title: "编辑服务类别",
	            type: 2,
	            content: "categorys/categorysEdit.html?id="+id,
	            success: function (layero, index) {

	            }
	        });

	        //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
	        $(window).resize(function () {
	            layui.layer.full(index);
	        });
	        layui.layer.full(index);
	  }
	    
	    function toDetail(id,mid){
		     var index = layui.layer.open({
		            title: "查看礼包",
		            type: 2,
		            content: "card/package_detail.html?id="+id+"&mid="+mid,
		            success: function (layero, index) {

		            }
		        });

		        //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
		        $(window).resize(function () {
		            layui.layer.full(index);
		        });
		        layui.layer.full(index);
	  }
	    
	    
	    function toNsDetail(id,mid){
		     var index = layui.layer.open({
		            title: "编辑礼包",
		            type: 2,
		            content: "card/package_ns_detail.html?id="+id+"&mid="+mid,
		            success: function (layero, index) {

		            }
		        });

		        //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
		        $(window).resize(function () {
		            layui.layer.full(index);
		        });
		        layui.layer.full(index);
	  }
	  
	  
	});
	

	
	
	
	</script> 
</body>
</html>